package com.oristand.aop;

import com.oristand.util.ResultResponse;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.sql.SQLException;

/**
 * 数据库报错
 * 用aop在controller进行环绕
 *
 * @author shiyjian
 * @version 1.0
 * @date 2021/12/2 17:53
 */
@Component
@Aspect
public class ControllerAop {

    @Around("execution(* com.oristand.controller.*.*(..))")
    public Object checkDataBaseAlive(ProceedingJoinPoint point) {
        ResultResponse<String> resultResponse = new ResultResponse<>();
        try {
            return point.proceed();
            //捕获sql异常，如果出现sql异常，直接返回错误信息
        } catch (SQLException e) {
            resultResponse.selectFail(ResultResponse.DATABASE_ERROR);
            e.printStackTrace();
            return resultResponse;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            resultResponse.selectFail(ResultResponse.SERVICE_ERROR);
            return resultResponse;
        }
    }
}
